<?php
class ModuleBase
{
	public static $type = 'master';
	
	//插入数据
	public static function queryInsert($row, $table, $reType='id')
	{
		if (!$row || !is_array($row)){
			return false;
		}
		foreach ($row as $key=>$val){
			$fields[] = "`{$key}`";
			$values[] = "`{$val}`";
		}
		$sql = "INSERT INTO `{$table}` (". implode(',', $fields) .")VALUES(". implode(',', $values) .")";
		$result = Db::db_write()->query($sql);
		if (!$result) {
			return false;
		}else {
			if ($reType == 'id'){
				return Db::db_write()->getLastInsertId();
			}elseif ($reType == 'affectedRows'){
				return Db::db_write()->getAffectedRows();
			}else {
				return $row;
			}
		}
	}
	
	public static function queryUpdate($row, $where, $table, $reType='affectRows')
	{
		$sql = self::buildUpdateSql($row, $where, $table);
		$result = Db::db_write()->query($sql);
		if (!$result) {
			return false;
		}else {
			return $reType = $reType=='affectRows' ? $result : $row;
		}
	}
	
	public function buildUpdateSql($row, $where, $table)
	{
		if (!isset($row) || !is_array($row)){
			return false;
		}
		foreach ($row as $key=>$val){
			$updates[] = "`{$key}`='".($val)."'";
		}
		if (!$where) {
			$wheres[] = array();
		}elseif (!is_array($where)){
			$wheres[] = $where;
		}else {
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
		}
		$sql = "UPDATE `{$table}` SET ". implode(',', $updates);
		if ($wheres) {
			$sql .= "WHERE ". implode('and', $wheres);
		}
		return $sql;
	}
	
	public function queryDelete($where, $table)
	{
		if(!$where || !is_array($where)){
			$wheres[] = $where;
		}else{
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
		}
		$sql = "DELETE FROM `{$table}` WHERE " . implode(' and ', $wheres);
		$result = Db::db_write()->query($sql);
		return $result;
	}
	
	public function querySelect($fields, $where, $table, $order=array(), $start=0, $limit=0)
	{
		if(is_array($where)){
			$wheres = array();
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
			$where = implode(' and ', $wheres);
		}
		if(is_array($order)){
			$orders = array();
			foreach ($order as $key=>$val){
				$orders[] = "`{$key}` {$val}";
			} 
			$order = implode(',', $orders);
		}
		$sql = "SELECT ".implode(',', $fields)." FROM `{$table}`";
		if ($where){
			$sql .= " WHERE {$where}";
		}
		if($order){
			$sql .= " ORDER BY {$order}";
		}
		if($limit){
			$sql .= " LIMIT {$start},{$limit}";
		}
		$result = Db::db_read()->query($sql);
		$ret = array();
		if($result){
			while ($row = mysql_fetch_assoc($result)){
				$ret[] = $row;
			}
		}
		return $ret;
	}
	
	public function querySelectOrder($fields, $where=1, $table='survey', $order, $start=0, $limit=20)
	{
		$sql = "SELECT ". implode(',', $fields) ." FROM `{$table}` WHERE ". $where ." ORDER BY ". $order;
		if ($limit){
			$sql .= "LIMIT {$start},{$limit}";
		} 
		$result = Db::db_read()->query($sql);
		$ret = array();
		if ($result){
			while ($row = mysql_fetch_assoc($result)){
				$ret[] = $row;
			}
		}
		return $ret;
	}
	
	public function querySelectSingle($fields, $where, $table, $order=array())
	{
		if(is_array($where)){
			$wheres = array();
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
			$where = implode(' and ', $wheres);
		}
		if (is_array($order)){
			$orders = array();
			foreach ($order as $key=>$val){
				$orders[] = "`{$key}` {$val}";
			}
			$order = implode(',', $orders);
		}
		$sql = "SELECT ".implode(',', $fields)." FROM `{$table}` WHERE " . $where;
		if ($order){
			$sql .= " ORDER BY {$order}";
		}
		$result = Db::db_read()->query($sql);
		$ret = array();
		if($result){
			$ret = mysql_fetch_assoc($result);
		}
		return $ret;
	}
	

	public function getRows($sql)
	{
		$result = Db::db_read()->query($sql);
		$ret = array();
		if(!$result || empty($result)){
			return $ret;
		}
		while ($row = mysql_fetch_assoc($result)){
			$ret[] = $row;
		}
		return $ret;
	}
	
	public function initDb($type='master')
	{
		self::$type = $type;
	}
	
	public function getRow($sql)
	{
		$result = Db::db_read()->query($sql);
		$row = array();
		if(!$result || empty($result)){
			return $row;
		}
		return mysql_fetch_assoc($result);
	}
	
	public function query($sql)
	{
		return Db::getInstance(self::$type)->query($sql);
	}
	
	public function getLastSql()
	{
		return Db::getInstance(self::$type)->getLastSql();
	}
	
	public function getError()
	{
		return Db::getInstance(self::$type)->getError();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}